from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin,CreateModelMixin
from rest_framework.viewsets import GenericViewSet

from .models import CourseCategory, Course, Teacher, Comment
from .serializers import CourseCategorySerializer, CourseSerializer, TeacherSerializer, CommentSerializer


class CourseCategoryView(GenericViewSet, ListModelMixin):
    queryset = CourseCategory.objects.all().filter(is_delete=False).order_by('orders')
    serializer_class = CourseCategorySerializer


class CourseView(GenericViewSet, ListModelMixin, RetrieveModelMixin):
    queryset = Course.objects.all().filter(is_delete=False).order_by('orders')
    serializer_class = CourseSerializer
    # 支持过滤，按课程分类过滤：需要借助于django-filter
    filter_backends = [DjangoFilterBackend]
    # http://127.0.0.1:8080/api/v1///?course_category=2
    filter_fields = ['course_category']


class TeacherView(GenericViewSet, ListModelMixin,RetrieveModelMixin):
    queryset = Teacher.objects.all().filter(is_delete=False).order_by('orders')
    serializer_class = TeacherSerializer


class CommentView(GenericViewSet, ListModelMixin,RetrieveModelMixin):
    queryset = Comment.objects.all().order_by('-create_time')
    serializer_class = CommentSerializer
    filter_backends = [DjangoFilterBackend]
    filter_fields = ['teacher']

class CommentaddView(GenericViewSet, CreateModelMixin):
    queryset = Comment.objects.all().order_by('-create_time')
    serializer_class = CommentSerializer
